package org.aeon.client.view.components.button;

import lombok.Getter;
import lombok.Setter;
import org.aeon.entity.dto.UserDto;
import org.aeon.utils.ImageSetUtil;
import org.aeon.utils.SwingUtil;

import javax.swing.*;
import java.awt.*;

/**
 * @author: MissingAeon
 * @date: 2024/2/29 14:51
 * @description:
 */
public class CheckButton extends JButton {
    private static final ImageIcon checkIcon;
    private static final ImageIcon checkFade;
    private static final ImageIcon unchecked;
    private static final ImageIcon cancelIcon;

    static {
        checkIcon = new ImageIcon(ImageSetUtil.getImage("check", 20, 20));
        checkFade = new ImageIcon(ImageSetUtil.getImage("check-fade", 20, 20));
        unchecked = new ImageIcon(ImageSetUtil.getImage("unchecked", 20, 20));
        cancelIcon = new ImageIcon(ImageSetUtil.getImage("cancel", 20, 20));
    }

    private final JPanel panel;

    @Getter
    private UserDto content;
    @Getter
    private boolean check;
    @Getter
    @Setter
    private CheckButton another;

    private CheckButton(Dimension dimension) {
        panel = new JPanel();
        panel.setLayout(null);

        panel.setOpaque(false);
        this.setBackground(Color.WHITE);

        this.add(panel);
        this.setMargin(new Insets(5, 5, 5, 5));
        SwingUtil.setSize(this, dimension);
    }

    public static CheckButton createCheckBox(Dimension dimension, UserDto content, boolean modifiable) {
        final CheckButton checkButton = new CheckButton(dimension);
        // 不可修改初始选中
        checkButton.check = !modifiable;
        checkButton.content = content;
        final JLabel checkLabel = new JLabel(modifiable ? unchecked : checkFade);
        checkLabel.setBounds(5, 8, 20, 20);
        checkButton.panel.add(checkLabel);

        final JLabel label = new JLabel(content.getNickname());
        label.setFont(new Font("黑体", Font.PLAIN, 14));
        label.setBounds(35, 3, dimension.width - 35 - 10, 30);
        checkButton.panel.add(label);

        if (modifiable) {
            checkButton.addActionListener(e -> {
                checkButton.check = !checkButton.check;
                checkLabel.setIcon((checkButton.check) ? checkIcon : unchecked);
            });
        }
        return checkButton;
    }

    public static CheckButton createRemoveBox(Dimension dimension, UserDto content) {
        final CheckButton checkButton = new CheckButton(dimension);
        checkButton.content = content;

        final JLabel label = new JLabel(content.getNickname());
        label.setFont(new Font("黑体", Font.PLAIN, 14));
        label.setBounds(5, 3, dimension.width - 35 - 10, 30);
        checkButton.panel.add(label);

        final JLabel checkLabel = new JLabel(cancelIcon);
        checkLabel.setBounds(dimension.width - 35 - 10, 8, 20, 20);
        checkButton.panel.add(checkLabel);

        return checkButton;
    }
}
